package cn.com.eado.crm.v.weiget

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.HorizontalScrollView
import kotlin.math.abs

class MyHorizontalScrollView(
    private val ctx: Context,
    private val attributeSet: AttributeSet?,
    defStyleAttr: Int) : HorizontalScrollView(ctx, attributeSet, defStyleAttr) {
    constructor(ctx: Context, attributeSet: AttributeSet?) : this(ctx, attributeSet, 0)
    constructor(ctx: Context) : this(ctx, null)

    private var lastX = 0f
    private var lastY = 0f

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        when(ev?.action){
            MotionEvent.ACTION_DOWN -> {
                lastX = ev.x
                lastY = ev.y
            }
            MotionEvent.ACTION_UP -> {
                lastX = 0f
                lastY = 0f
            }
            MotionEvent.ACTION_MOVE -> {
                val dx = ev.x - lastX
                val dy = ev.y - lastY
                return abs(dx) > abs(dy)
            }
        }
        return super.onInterceptTouchEvent(ev)
    }
}